﻿using DDU.Infrastructure;
using System.Net;
using System.IO;
using System;
using System.IO.IsolatedStorage;
using Microsoft.Phone.Net.NetworkInformation;

namespace DDU.DataModel
{
    public class RecommendBooksInfo : NotificationObject
    {
        WebClient client = new WebClient();

        public RecommendBooksInfo()
        {
            client.OpenReadCompleted -= client_OpenReadCompleted;
            client.OpenReadCompleted += client_OpenReadCompleted;
        }

        void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                Stream = e.Result;
                RaisePropertyChanged(() => Stream);

                string key = DeviceNetworkInformation.IsWiFiEnabled ? OptionKeys.WiFiNetworkDataLength : OptionKeys.CellNetworkDataLength;

                Int64 length = Convert.ToInt64(IsolatedStorageSettings.ApplicationSettings[key]);
                IsolatedStorageSettings.ApplicationSettings[key] = length + e.Result.Length;
                IsolatedStorageSettings.ApplicationSettings.Save();
            }
        }

        private string _bookName;
        public string BookName
        {
            get { return _bookName; }
            set
            {
                _bookName = value;
                RaisePropertyChanged(() => BookName);
            }
        }

        private string _author;
        public string Author
        {
            get { return _author; }
            set
            {
                _author = value;
                RaisePropertyChanged(() => Author);
            }
        }

        public string _press;
        public string Press
        {
            get { return _press; }
            set
            {
                _press = value;
                RaisePropertyChanged(() => Press);
            }
        }

        private string _barcode;
        public string Barcode
        {
            get { return _barcode; }
            set
            {
                _barcode = value;
                RaisePropertyChanged(() => Barcode);
            }
        }

        private string _isbn;
        public string ISBN
        {
            get { return _isbn; }
            set
            {
                _isbn = value;
                RaisePropertyChanged(() => ISBN);
            }
        }

        private double _price = 0.00;
        public double Price
        {
            get { return _price; }
            set
            {
                _price = value;
                RaisePropertyChanged(() => Price);
            }
        }

        private double _preferential = 0.00;
        public double Preferential
        {
            get { return _preferential; }
            set
            {
                _preferential = value;
                RaisePropertyChanged(() => Preferential);
            }
        }

        private string _url;
        public string Url
        {
            get { return _url; }
            set
            {
                _url = value;
                RaisePropertyChanged(() => Url);
            }
        }

        private string _image;
        public string Image
        {
            get { return _image; }
            set
            {
                _image = value;
                RaisePropertyChanged(() => Image);
                if (DeviceNetworkInformation.IsNetworkAvailable)
                {
                    client.OpenReadAsync(new Uri(Image, UriKind.RelativeOrAbsolute));
                }
            }
        }

        public Stream Stream { get; set; }
    }
}
